package com.ksyun.start.camp.service.impl;

import com.ksyun.start.camp.service.SimpleTimeService;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * 代表简单时间服务实现
 */
@Component
public class SimpleTimeServiceImpl implements SimpleTimeService {

    @Override
    public String getDateTime(String style) {
        // 开始编写简单时间服务的核心逻辑
        // 获取时间、格式化时间、返回格林尼治标准时间（GMT）
        LocalDateTime localTime = LocalDateTime.now(ZoneOffset.UTC);
        String result;
        switch (style) {
            case "full":
                result = localTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                break;
            case "date":
                result = localTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                break;
            case "time":
                result = localTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
                break;
            case "unix":
                Instant instant = localTime.toInstant(ZoneOffset.UTC);
                long unixTimestampMillis = instant.toEpochMilli();
                result = String.valueOf(unixTimestampMillis);
                break;
            default:
                result = "请选择返回的日期格式: full, date, time, unix";
                break;
        }
        return result;
    }
}
